package com.snake.design.pe.reflect;
/**
 * 1.反射三种方式分别是类加载机制 class.forName("");
 * 2.通过类属性class,XXX.class;
 * 3.通过对象属性new A().getClass();
 * 最后调用newInstance()方法获取对象实例必须有无参构造器
 * @author fengx00
 *
 */
public class ReflectDemo {
	private Integer id;
	private String name;

	public ReflectDemo() {
		super();
	}

	public ReflectDemo(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void say() {
		System.out.println("java reflect");

	}

	// 类加载器反射获取对象
	public static void reflect1() throws Exception {
		Class<?> c = Class.forName("com.snake.design.pe.reflect.ReflectDemo");
		System.out.println(c.getClassLoader());// sun.misc.Launcher 程序入口
		ReflectDemo re = (ReflectDemo) c.newInstance();// 需要空构造器
		re.say();
	}

	public static void reflect2() throws Exception {
		ReflectDemo re = ReflectDemo.class.newInstance();// 每个类都有属性class 一般叫做类类型
		System.out.println(ReflectDemo.class.getClassLoader());// sun.misc.Launcher
																// 程序入口
		re.say();
	}

	public static void reflect3() throws Exception {
		ReflectDemo r = new ReflectDemo();
		ReflectDemo r2 = r.getClass().newInstance();
		r2.say();
	}

	public static void main(String[] args) throws Exception {
		// reflect1();
		// reflect2();
		reflect3();
	}
}
